package com.yupi.project.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guazai.common.pojo.InterfaceInfo;
import com.guazai.common.pojo.User;
import com.qiteng.apiclientsdk.client.ApiClient;
import com.yupi.project.common.ErrorCode;
import com.yupi.project.exception.BusinessException;
import com.yupi.project.mapper.InterfaceInfoMapper;
import com.yupi.project.model.dto.interfaceinfo.InterfaceInfoInvokeRequest;
import com.yupi.project.model.enums.InterfaceInfoStatusEnum;
import com.yupi.project.model.vo.AnalysisVO;
import com.yupi.project.service.InterfaceInfoService;
import com.yupi.project.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author yiyan
 * @description 针对表【interface_info(接口信息)】的数据库操作Service实现
 * @createDate 2024-07-14 00:01:01
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoService {

    @Resource
    UserService userService;

    @Resource
    InterfaceInfoMapper interfaceInfoMapper;

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String name = interfaceInfo.getName();
        // 创建时，所有参数必须非空
        if (add) {
            if (StringUtils.isAnyBlank(name)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
    }

    @Override
    public String invokeInterfaceInfo(InterfaceInfoInvokeRequest invokeRequest, HttpServletRequest request) {
        long id = invokeRequest.getId();
        // 1. 请求的模拟接口是否存在，状态是否可用，以及请求方法是否匹配
        // 判断是否存在
        InterfaceInfo oldInterfaceInfo = this.getById(id);
        if (oldInterfaceInfo == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        // 判断接口状态是否可以调用
        if (oldInterfaceInfo.getStatus() == InterfaceInfoStatusEnum.OFFLINE.getValue()) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "接口已关闭");
        }

        //调用接口
        User loginUser = userService.getLoginUser(request);
        String accessKey = loginUser.getAccessKey();
        String secretKey = loginUser.getSecretKey();
        ApiClient tempClient = new ApiClient(accessKey, secretKey);
        String params = invokeRequest.getUserRequestParams();
        // todo 改善：动态调用接口
        String result = tempClient.invokeInterface(params, oldInterfaceInfo.getPath());
        System.out.println("调用成功！");
        return result;
    }

    @Override
    public List<AnalysisVO> getTopInterfaceInfo() {
        return interfaceInfoMapper.getTopInterfaceInfo(3);
    }
}




